package java67_adv9;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

//聊天的线程
public class FileThread extends Thread {
	private Socket s;
	static byte[] b;
	static {
		try {
			FileInputStream fin = new FileInputStream("E:\\BaiduNetdiskDownload\\job-salary.png");
			b = new byte[fin.available()];
			fin.read(b);
			fin.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public FileThread(Socket s) {
		this.s = s;
		super.setName(String.valueOf(s.getInetAddress()));

	}

	@Override
	public void run() {
		System.out.println(s.getInetAddress() + ":" + s.getPort() + "连接上了...");
		try {
			// InputStream in = s.getInputStream();
			OutputStream out = s.getOutputStream();
			out.write(b);
			out.flush();
			System.out.println("文件发送给" + s.getInetAddress() + "成功");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (s != null)
				try {
					s.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}
}
